# 使用官方的 Playwright Python 镜像
FROM mcr.microsoft.com/playwright/python:v1.44.0-jammy

# 设置工作目录
WORKDIR /app

# ==================== 终极武器：安装 Google Chrome ====================
# 安装必要的依赖，并添加 Google 的官方 GPG 密钥
RUN apt-get update && apt-get install -y \
    wget \
    gnupg \
    --no-install-recommends \
    && wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/google-chrome-keyring.gpg \
    && echo "deb [arch=amd64 signed-by=/usr/share/keyrings/google-chrome-keyring.gpg] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list

# 更新 apt 列表并安装稳定版的 Google Chrome
RUN apt-get update && apt-get install -y \
    google-chrome-stable \
    --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*
# =====================================================================

# 创建并设置持久化数据目录的权限
RUN mkdir -p /app/browser_data && chown -R pwuser:pwuser /app/browser_data

# 复制依赖文件并安装
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# 复制所有项目文件到工作目录
COPY . .

# 切换到非 root 用户
USER pwuser

# 暴露端口
EXPOSE 8000

# 启动应用
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
